using System;
using System.Data;

using System.Configuration;

using System.Data.OleDb;
using Entity;

namespace SalesVMT
{
    public class CustomerDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SELECT * FROM SalesVMT_Customer", dbConn);
            dbCmd.CommandType = CommandType.Text;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetAllFromShipment()
        {
            DataSet retVal = null;
            string sql = "SELECT Customer_ID, Customer_Code, Customer_Name, Customer_Contact, Customer_Address " +
                        " FROM vw_SalesVMT_Shipment " +
                        " WHERE (Shipment_Pay =0 Or Shipment_Pay < Shipment_Total) and Shipment_Total > 0" +
                        " GROUP BY Customer_ID, Customer_Code, Customer_Name, Customer_Contact, Customer_Address" +
                        " ORDER BY Customer_Name";
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand(sql, dbConn);
            dbCmd.CommandType = CommandType.Text;
            try
            {
                retVal = new DataSet();
                OleDbDataAdapter da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, int customertype_ID, string keyWord, string orderBy, string orderDirection, int currentPage, int pageSize)
        {
            string sql = "";
            string strWhere = "";

            string order = "";
            int total = 0;
            total = currentPage * pageSize;
            if (order == "")
                order = "Customer_ID DESC";
            else
                order = orderBy + " " + orderDirection;


            strWhere = " Where 0 = 0";
            if (keyWord.Length > 0)
            {
                strWhere = strWhere + " and (Customer_Code Like N''%' + @Keyword + '%''" +
                                        " or Customer_Name Like N''%' + @Keyword + '%'')'";
            }
            if (customertype_ID > 0)
            {
                strWhere = strWhere + " and Customer_CustomerTypeID=" + customertype_ID.ToString();
            }
            
            
            sql = "Select top " + pageSize.ToString() + " * FROM SalesVMT_Customer";

            if (total > 0)
            {
                sql = sql + strWhere;
                sql = sql + " And Customer_ID NOT IN (SELECT TOP " + total.ToString() + " Customer_ID FROM SalesVMT_Customer" + strWhere + " Order By " + order + ")";
            }
            else
                sql = sql + strWhere;

            sql = sql + " Order By " + order;



            OleDbConnection dbConn = clsConnection.CreateConnection();
            DataSet retVal = null;
            DataSet retVal1 = null;
            OleDbCommand dbCmd;
            OleDbDataAdapter da;

            try
            {

                dbCmd = new OleDbCommand(sql, dbConn);
                dbCmd.CommandType = CommandType.Text;
                retVal = new DataSet();
                da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal);

                dbCmd = new OleDbCommand("Select count(*) from  SalesVMT_Customer" + strWhere, dbConn);
                dbCmd.CommandType = CommandType.Text;
                retVal1 = new DataSet();
                da = new OleDbDataAdapter(dbCmd);
                da.Fill(retVal1);

                ItemCount = Convert.ToInt32(retVal1.Tables[0].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Delete(int _customer_ID)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Customer_ID", _customer_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(CustomerInfo _CustomerInfo)
        {
            int retVal = 0;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Customer_CustomerTypeID", _CustomerInfo.Customer_CustomerTypeID);
            dbCmd.Parameters.AddWithValue("@Customer_Code", _CustomerInfo.Customer_Code);
            dbCmd.Parameters.AddWithValue("@Customer_Name", _CustomerInfo.Customer_Name);
            dbCmd.Parameters.AddWithValue("@Customer_Address", _CustomerInfo.Customer_Address);
            dbCmd.Parameters.AddWithValue("@Customer_Phone", _CustomerInfo.Customer_Phone);
            dbCmd.Parameters.AddWithValue("@Customer_Fax", _CustomerInfo.Customer_Fax);
            dbCmd.Parameters.AddWithValue("@Customer_TaxNo", _CustomerInfo.Customer_TaxNo);
            dbCmd.Parameters.AddWithValue("@Customer_Contact", _CustomerInfo.Customer_Contact);
            dbCmd.Parameters.AddWithValue("@Customer_Remaining", _CustomerInfo.Customer_Remaining);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Update(CustomerInfo _CustomerInfo)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_Update", dbConn);

            dbCmd.Parameters.AddWithValue("@Customer_ID", _CustomerInfo.Customer_ID);
            dbCmd.Parameters.AddWithValue("@Customer_CustomerTypeID", _CustomerInfo.Customer_CustomerTypeID);
            dbCmd.Parameters.AddWithValue("@Customer_Code", _CustomerInfo.Customer_Code);
            dbCmd.Parameters.AddWithValue("@Customer_Name", _CustomerInfo.Customer_Name);
            dbCmd.Parameters.AddWithValue("@Customer_Address", _CustomerInfo.Customer_Address);
            dbCmd.Parameters.AddWithValue("@Customer_Phone", _CustomerInfo.Customer_Phone);
            dbCmd.Parameters.AddWithValue("@Customer_Fax", _CustomerInfo.Customer_Fax);
            dbCmd.Parameters.AddWithValue("@Customer_TaxNo", _CustomerInfo.Customer_TaxNo);
            dbCmd.Parameters.AddWithValue("@Customer_Contact", _CustomerInfo.Customer_Contact);
            dbCmd.Parameters.AddWithValue("@Customer_MoneySend", _CustomerInfo.Customer_Remaining);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static CustomerInfo GetInfo(int _customer_ID)
        {
            CustomerInfo retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Customer_ID", _customer_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            OleDbDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CustomerInfo();
                    retVal.Customer_ID = Convert.ToInt32(dr["Customer_ID"]);
                    retVal.Customer_CustomerTypeID = Convert.ToInt32(dr["Customer_CustomerTypeID"]);
                    retVal.Customer_Code = Convert.ToString(dr["Customer_Code"]);
                    retVal.Customer_Name = Convert.ToString(dr["Customer_Name"]);
                    retVal.Customer_Address = Convert.ToString(dr["Customer_Address"]);
                    retVal.Customer_Phone = Convert.ToString(dr["Customer_Phone"]);
                    retVal.Customer_Fax = Convert.ToString(dr["Customer_Fax"]);
                    retVal.Customer_TaxNo = Convert.ToString(dr["Customer_TaxNo"]);
                    retVal.Customer_Contact = Convert.ToString(dr["Customer_Contact"]);
                    retVal.Customer_Remaining = Convert.ToDecimal(dr["Customer_Remaining"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Using(int CustomerID)
        {
            int retVal = 0;
            OleDbConnection dbConn = clsConnection.CreateConnection();
            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Customer_ID", CustomerID);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Exist(int CustomerID, string CustomerName)
        {
            bool retVal = false;
            OleDbConnection dbConn = clsConnection.CreateConnection();

            OleDbCommand dbCmd = new OleDbCommand("SalesVMT_Customer_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Customer_Name", CustomerName);
            dbCmd.Parameters.AddWithValue("@Customer_ID", CustomerID);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
